<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="角色名" prop="roleName">
                <el-input v-model="ruleForm.roleName"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="ruleForm.remark"></el-input>
            </el-form-item>
        </el-form>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <el-button @click="submitForm('ruleForm')">提交</el-button>
    </div>
</template>
<script>
    export default {
        name:"addRoles",
        data() {
            return {
                ruleForm: {
                    roleName: '',
                    remark:''
                },
                rules: {
                    roleName: [
                        {required: true, message: '请输入角色名', trigger: 'blur'},
                        {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                    ],
                }
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        //数据组装
                        let roles = new FormData();

                        Object.keys(this.ruleForm).forEach(key => {
                            roles.append(key, this.ruleForm[key])
                        })

                        this.$axios.post("addroles.action",roles).
                            then(request=>{
                            if (request.data.msg=="添加成功"){
                                this.$message.success("添加成功")
                                this.$parent.$parent.getdata();
                                this.$parent.$parent.dialogVisible = false
                                this.reset();
                            }else {
                                this.$message.error("添加失败")
                            }
                        }).catch()
                    }
                })
            },
            reset(){
                this.ruleForm.roleName = ""
                this.ruleForm.remark = ""
            }
        }
    }
</script>

<style scoped>

</style>